#include <QDebug>
#include <QString>
#include <QXmlStreamWriter>
#include <QIODevice>
#include <QFile>
#include <QFileDialog>
#include <QMessageBox>
#include "world.h"
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "ui_gamewindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    settings = new GameSettings();
    highscores = new Highscores();
    game = new GameWindow(settings, highscores);


    connect(game, SIGNAL(updateWindowState(bool,bool,bool, bool)),
            this, SLOT(updateWindowVisibility(bool,bool,bool, bool)));
    connect(settings, SIGNAL(updateWindowState(bool,bool,bool, bool)),
            this, SLOT(updateWindowVisibility(bool,bool,bool, bool)));
    connect(highscores, SIGNAL(updateWindowState(bool,bool,bool, bool)),
            this, SLOT(updateWindowVisibility(bool,bool,bool, bool)));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_btnNewGame_clicked()
{
    settings->setWindowFlags(Qt::WindowStaysOnTopHint);
    settings->show();
    settings->updateWidgetState();
}

void MainWindow::on_btnResumeGame_clicked()
{
    if (game->getGameInProgress()) {
        this->hide();
        game->show();
        game->setFocus();
        game->setGamePaused(false);
    }
}

void MainWindow::on_btnInstructions_clicked()
{
    helpscreen instructions;
    instructions.setModal(true);
    instructions.setWindowFlags(Qt::WindowStaysOnTopHint);
    instructions.exec();
}

void MainWindow::updateWindowVisibility(bool m, bool g, bool s, bool h)
{
    if (m) { this->show(); ui->btnResumeGame->setEnabled(game->getGameInProgress()); } else { this->hide(); }
    if (g) { game->show(); game->setFocus(); } else { game->hide(); }
    if (s) { settings->show(); } else { settings->hide(); }
    if (h) { highscores->show(); } else { highscores->hide(); }
}

void MainWindow::on_btnHighscores_clicked()
{
    highscores->setWindowFlags(Qt::WindowStaysOnTopHint);
    highscores->show();
}

void MainWindow::on_btnQuit_clicked()
{
    QApplication::quit();
}

void MainWindow::closeEvent(QCloseEvent* event)
{
    (void)event;
    QApplication::quit();
}



/*void MainWindow::saveXMLFile()
{
    World dataWorld = World::getWorld();

    for (int i=0; i < dataWorld.getObjects().size(); ++i)
    {
        //implement
    }


    QString filename = QFileDialog::getSaveFileName(this, tr("Save Xml"), ".", tr("Xml files (*.xml)"));


    QFile file (filename);
    file.open(QIODevice::WriteOnly);

    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();

    file.close();
}

void MainWindow::readXMLFile()
{
    QXmlStreamReader Rxml;

    QString filename = QFileDialog::getOpenFileName(this, tr("Open Xml"), ".", tr("Xml files (*.xml)"));
    QFile file(filename);

    if (!file.open(QFile::ReadOnly | QFile::Text))
    {
        QMessageBox::warning(this, "error", "error");

    }

    Rxml.setDevice(&file);
    Rxml.readNext();
}*/
